Skip to content

feat(llamaapi): enable structured_output support #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sssure02
Copy link

@sssure02 sssure02 commented Aug 22, 2025

Description

Feature: Enables structured_output support for LLamaAPI

Summary

This PR enables structured output handling for the LlamaAPI model provider, aligning it with the same pattern used in OpenAI, Bedrock, Ollama and SageMaker implementations. Unit tests for structured output are also added.

Related Issues

Resolves #257

Changes Made

1. Uncomment code in structured_output method to LlamaAPI model provider and include addtional features

File: src/strands/models/llamaapi.py
Purpose: Enable structured output parsing directly from LlamaAPI responses
Additional Features:

  • Safely handles completion_message.content by checking for None, str, or MessageTextContentItem types
  • Parses JSON response and instantiates the specified Pydantic output model
  • Uses yield to return structured results as an async generator, aligning with other model providers and allowing event-based handling
  • Implements error handling

2. Add test_structured_output test case for LlamaAPI structured output

File: tests/strands/models/test_llamaapi.py
Purpose: Validate correct parsing and instantiation of structured outputs from LlamaAPI responses

3. Add test_agent_structured_output test cases for LlamaAPI structured output

File: tests_integ/strands/models/test_model_llamaapi.py
Purpose: Validate agent-level structured output functionality for both sync and async execution

All test cases have passed

Usage Example

from pydantic import BaseModel
from strands import Agent
from strands.models.llamaapi import LlamaAPIModel

class Address(BaseModel):
    street: str
    city: str
    state: str
    zip: str

model = LlamaAPIModel(
    client_args={"api_key": os.getenv("LLAMA_API_KEY")},
    model_id="Llama-4-Maverick-17B-128E-Instruct-FP8",
)

agent = Agent(model=model)

result = agent.structured_output(Address,"Analyze this address: 123 Main St, Anytown, Anystate 12345")

print(f"Street: {result.street}")
print(f"City: {result.city}")
print(f"State: {result.state}")
print(f"Zip: {result.zip}")

Output

Street: 123 Main St
City: Anytown
State: Anystate
Zip: 12345

Documentation PR

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Implement Llama model provider structued_output
1 participant